package org.flower.web.servlet;

import com.alibaba.fastjson.JSON;
import org.flower.bean.CommodityClass;
import org.flower.service.CommodityClassService;
import org.flower.service.impl.CommodityClassServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

/**
 * @author Aizen
 */
@WebServlet("/manager/commodityClassAjaxServlet")
public class CommodityClassAjaxServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {




    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CommodityClassService classService = new CommodityClassServiceImpl();

        //Ajax获取到 classID 和 该ID的类型
        String classId = request.getParameter("classId");
        String type = request.getParameter("type");

        //如果是二级菜单改变事件
        if(type.equals("type2")){
            //值不等于空 应当按二级菜单查询
            if(classId!=null&&!classId.equals("")){
                //在作用域放上二级菜单查询标记
                request.getSession().setAttribute("type2classId",classId);
            }else{
                //如果值为空，应按一级菜单查询 并清除二级菜单查询标记
                request.getSession().removeAttribute("type2classId");
            }
        }else{
            //如果是一级菜单改变事件 清除二级菜单查询标记
            request.getSession().removeAttribute("type2classId");
            //在作用域放上一级菜单查询标记
            request.getSession().setAttribute("type1classId",classId);

            if(classId==null||classId.equals("")){
                response.getWriter().print(true);
                return;
            }

        }

        //查询所有级别返回
        List<CommodityClass> classByPid = classService.findClassByPid(Integer.parseInt(classId));
        String s = JSON.toJSONString(classByPid);
        response.getWriter().print(s);

    }
}
